import { ref } from 'vue'
import { Toast } from 'vant';
import { RequestProductSearch } from '@/api/search'
import { useProductDetail } from '@/hooks/home'
import { useRouter } from 'vue-router'
import 'vant/es/toast/style';

export const useSearch = () => {
    const searchValue = ref('')
    const { onDetail } = useProductDetail()
    let searchList = ref([])
    const router = useRouter()

    const onClickLeft = () => history.back();
    const onSearch = async () => {//点击键盘上的搜索或回车按钮后触发
        // router.push({ path: '/results/' })
        let productSearch = searchValue.value
        let data = await RequestProductSearch(productSearch)
        const { resultCode, resultInfo } = data
        if (resultCode == 1) {
            searchList.value = resultInfo.list
        } else {
            // alert('没有该商品')
            searchList.value = ''
            Toast.fail('没有该商品')
        }
    }
    const onClickButton = async () => {
        // router.push({ path: '/results/' })
        let productSearch = searchValue.value
        let data = await RequestProductSearch(productSearch)
        const { resultCode, resultInfo } = data
        if (resultCode == 1) {
            searchList.value = resultInfo.list
        } else {
            // alert('没有该商品')
            searchList.value = ''
            Toast.fail('没有该商品')
        }
    }

    return {
        onClickLeft,
        searchValue,
        onSearch,
        onClickButton,
        searchList,
        onDetail,
    }
}



